package com.yuelao.thread.demo;

import java.util.concurrent.Semaphore;

/**
 * 线程并发数量控制
 * Created by fengliye on 2017/10/7.
 */
public class SemaphoreTest {
    private static final int THREAD_COUNT = 30;

    private static Semaphore s = new Semaphore(10);

    public static void main(String[] args) {
        for (int i = 0; i < THREAD_COUNT; i++) {
            new Thread(() -> {
                try {
                    s.acquire();
                    System.out.println("save data");
                    Thread.sleep(2000);
                    s.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }).start();
        }
    }
}
